package com.warframe.cycletime;

import com.alibaba.fastjson.JSON;
import com.tools.StringContent;
import com.warframe.entity.cycletime.CetusTimeMO;
import com.warframe.entity.message.Message;
import com.warframe.message.Executor;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class CycleTime implements Runnable {
    private static String url = "https://api.warframestat.us/pc/cetusCycle";
    private Message message;
    Long timestamp;


    public CycleTime(Message message) {
        this.message = message;
    }

    private void getResult() {
        Long currentTimeStamp = System.currentTimeMillis() / 1000;
        timestamp = (currentTimeStamp - StringContent.CYCLETIMESTAMP.get(StringContent.CETUS)) % 9000;
    }

    private void explain() {
        StringBuilder cetusTimesString = new StringBuilder("指挥官，Ordis检测到当前希图斯时间\nDAYSTATUS，距离NEXTSTATUS还有");
        String result;
        if (timestamp > 3000) {
            timestamp = 6000 - (timestamp - 3000);
            Long hour = timestamp / 3600;
            Long minute = timestamp / 60 % 60;
            Long second = timestamp % 60;
            cetusTimesString.append("[ "+ hour + " 小时 " + minute + " 分钟 " + second + " 秒 ]");
            result = cetusTimesString.toString().replace("DAYSTATUS","白天")
                    .replace("NEXTSTATUS","夜晚");
        } else {
            timestamp = 3000 - timestamp;
            Long minute = timestamp / 60 % 60;
            Long second = timestamp % 60;
            cetusTimesString.append("[ " + minute + " 分钟 " + second + " 秒 ]");
            result = cetusTimesString.toString().replace("DAYSTATUS","夜晚")
                    .replace("NEXTSTATUS","白天");
        }
        if (message.getGroupId() != null) {
            message.setSign(StringContent.SENDGROUP);
        } else {
            message.setSign(StringContent.SENDPERSON);
        }
        message.setMsg(result);
        Executor.getInstance().pushMsg(message);
    }

    @Override
    public void run() {
        getResult();
        explain();
//        Long currentTimeStamp = System.currentTimeMillis() / 1000;
//        Long lastTimeStamp = StringContent.CYCLETIMESTAMP.get(StringContent.CETUSNEW);
//        if (lastTimeStamp == null ||
//                (lastTimeStamp != null && currentTimeStamp > lastTimeStamp)) {
//            getCycleTime();
////        }
//        explain();
    }
}
